package com.atzxh.guigu.juc.auxiliary;

import java.util.concurrent.CountDownLatch;

/**
 * @author 小红同学
 * @create 2023-04-27-21:48
 * @description     演示 CountDownLatch
 */
public class CountDownLatchDemo {
    // 六个同学陆续离开教室，走完班长锁门
    public static void main(String[] args) throws InterruptedException {
        // 创建 CountDownLatch 对象，并设置初始值
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i <= 6 ; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName() + "  号同学离开教室");
                // 计数减一
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }
        // 等待
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + "  班长锁门");
    }
}
